//
//  CCViewController.swift
//  SwiftZCC
//
//  Created by Mac mini on 2020/3/20.
//  Copyright © 2020 Mr.Hong. All rights reserved.
//

import UIKit


// MARK: - 弹窗
public extension UIViewController {
    /// 显示Alter样式
    func cnm_showAlert(title: String = "温馨提示", message: String?, cancelTitle: String? = nil, sureTitle: String? = nil, cancelHandler: ((UIAlertAction)->Void)? = nil, sureHandler: ((UIAlertAction)->Void)? = nil) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        
        if cancelTitle != nil {
            let cancelItem = UIAlertAction(title: cancelTitle, style: .destructive, handler: cancelHandler)
            alertController.addAction(cancelItem)
        }
        if sureTitle != nil {
            let sureItem = UIAlertAction(title: sureTitle, style: .default, handler: sureHandler)
            alertController.addAction(sureItem)
        }
        
        present(alertController, animated: true, completion: nil)
    }
    
    /// 显示Sheet样式
    func cnm_showSheet(title: String? = nil, message: String? = nil, items: [String], itemHandler: ((UIAlertAction)->Void)?) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
        
        items.forEach { value in
            let item = UIAlertAction(title: value, style: .default, handler: itemHandler)
            alertController.addAction(item)
        }
        let cancelItem = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        cancelItem.setValue(UIColor.red, forKey: "_titleTextColor")
        alertController.addAction(cancelItem)
        
        present(alertController, animated: true, completion: nil)
    }
}
